﻿using Autofac;
using Blm.VisionCloud.FrameWork.Interceptor.Log;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Hosting;
using System.IdentityModel.Tokens.Jwt;
using VisionCloud.Core;
using VisionCloud.Domain.UnitOfWork;
using VisionCloud.Utility;
using VisionCloud.Web.Authorize;
using VisionCloud.Web.Service;

namespace VisionCloud.Web.Config
{
    /// <summary>
    /// Ioc配置:包含各服务类对象、数据库上下文对象、缓存对象、后台服务
    /// </summary>
    public class IocConfig
    {
        public static void Init(ContainerBuilder builder)
        {
            //builder.RegisterType<JwtTokenAuthorizationHandler>().As<IAuthorizationHandler>().SingleInstance();
            builder.RegisterModelWithInterface("VisionCloud.Domain", w => w.Name.EndsWith("Repository") && !w.IsInterface);
            builder.RegisterController("VisionCloud.Api");
            builder.RegisterModel("VisionCloud.Service", w => w.Name.EndsWith("Service"));
            builder.RegisterType<PGUnitOfWork>().As<IUnitOfWork>().PropertiesAutowired().InstancePerLifetimeScope();
            //builder.RegisterType<JwtSecurityTokenHandler>().SingleInstance();
            builder.Register(c => new BlueDbContextFactory().CreateDbContext(new string[] { "1" })).As<BlueDbContext>().PropertiesAutowired().InstancePerLifetimeScope();
            builder.RegisterType<LoggerInterceptor>().PropertiesAutowired().InstancePerLifetimeScope();
            builder.RegisterType<LoggerAsyncInterceptor>().PropertiesAutowired().InstancePerLifetimeScope();

            //是否启动后台服务，如果不启动gethealth拿不到数据
            if (ConfigHelper.GetValue<bool>("BackgroundService"))
            {
                builder.RegisterType<UnzipTaskDataService>().As<IHostedService>().SingleInstance();
                builder.RegisterType<FileUploadQueueBackService>().As<IHostedService>().SingleInstance();
                //定时删除minio中下载和分享临时文件
                builder.RegisterType<SharingFilesDeleteService>().As<IHostedService>().SingleInstance();
                //定时删除日志文件
                builder.RegisterType<DeleteLogFileService>().As<IHostedService>().SingleInstance();

                builder.RegisterType<UnzipComputeData>().As<IHostedService>().SingleInstance();
            }
        }
    }
}